package com.heima.wemedia.controller;

import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.media.dtos.WmMaterialDto;
import com.heima.wemedia.pojo.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Fuqiang
 * @since :2023-03-25
 */

@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {

    @Autowired
    WmMaterialService materialService;

    /**
     * 分页当前用户的查询文章素材列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public PageResult<WmMaterial> list(@RequestBody WmMaterialDto dto) {

        return materialService.findByPage(dto);

    }

    /**
     * 上传图片到oss云存储
     *
     * @param file
     * @return
     */
    @PostMapping("/upload_picture")
    public ResponseResult<String> uploadPicture(@RequestParam("multipartFile") MultipartFile file) {

        return this.materialService.uploadPicture(file);
    }

    /**
     * 收藏素材
     *
     * @param id
     * @return
     */
    @PutMapping("/collect/{id}")
    public ResponseResult collect(@PathVariable("id") Integer id) {

        this.materialService.setIsCollectionById(id, true);
        return ResponseResult.ok();
    }

    /**
     * 取消收藏素材
     *
     * @param id
     * @return
     */
    @PutMapping("/cancel_collect/{id}")
    public ResponseResult cancelCollect(@PathVariable("id") Integer id) {
        this.materialService.setIsCollectionById(id, false);
        return ResponseResult.ok();
    }

    /**
     * 删除自媒体用户素材, 被文章引用的素材不能被删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/del_picture/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        this.materialService.deleteMaterial(id);
        return ResponseResult.ok();
    }
}
